<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-03-02 16:16:35 apple $
 第三方平台账号登陆
*/
defined('BY_JYA') or exit('error');

//签名校验
$open=pdo_get('core_open',array('token'=>trim($_GET['token'])));
if(!$open){
	exit('找不到指定第三方平台！');
}
$check_sign=$_GET['sign'];
if(!$check_sign){
	exit('缺少签名');
}
unset($_GET['sign']);
$sign=get_sign($_GET,$open['secret']);
if($sign!=$check_sign){
	exit('签名校验失败！');
}
//检查用户是否存在，如果不存在，则创建用户
if(empty($_GET['uid'])){
	exit('没有获取到uid！');
}
$open_user=pdo_get('core_open_user',array('open_id'=>$open['id'],'open_uid'=>intval($_GET['uid'])),array('uniacid','id'));
$uniacid=$open_user['uniacid'];
if(!$uniacid){
	do{
		$username='wx'.$_GET['uid'].'-'.random(5);
		$check=pdo_getcolumn('core_users',array('name'=>$username),'id');
	}while($check);
	$member=array('name'=>$username);
	$uniacid=cfc('user')->create_user($member);
	$user=array(
		'uniacid'=>$uniacid,
		'open_id'=>$open['id'],
		'open_uid'=>intval($_GET['uid']),
	);
	$id=pdo_insert('core_open_user',$user);
}else{
	$id=$open_user['id'];
}
//登陆用户
cfc('user')->login($uniacid);
$_SESSION['openid']=$id;
$_SESSION['access']=array();
if($_GET['access']){
	$access=json_decode(base64_decode($_GET['access']),true);
	$_SESSION['access']=array_fields(array('plugin','action','op','query'),$access);
}
//跳转首页
header('Location:./index.php');
